﻿using Manager.Model;
using Microsoft.Extensions.Options;
using RabbitMQ.Client;
using System.Text;

namespace Manager.Service
{
    public class RabbitMqService
    {
        private readonly IConnection _connection;

        public RabbitMqService(IOptions<RabbitMQOptions> options)
        {
            var factory = new ConnectionFactory()
            {
                HostName = options.Value.Host,
                Port = options.Value.Port,
                UserName = options.Value.UserName,
                Password = options.Value.Password
            };
            _connection = factory.CreateConnection();
        }


        //发送
        public void SendMessage(string message)
        {
            using var channel = _connection.CreateModel();
            channel.QueueDeclare(queue: "testQueue", durable: false, exclusive: false, autoDelete: false);
            var body = Encoding.UTF8.GetBytes(message);
            channel.BasicPublish(exchange: "", routingKey: "testQueue", basicProperties: null, body: body);
        }
    }
}
